package com.lemon.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.reggie.common.BusinessException;
import com.lemon.reggie.pojo.dto.CategoryDTO;
import com.lemon.reggie.pojo.entity.Category;
import com.lemon.reggie.mapper.CategoryMapper;
import com.lemon.reggie.pojo.entity.Dish;
import com.lemon.reggie.pojo.entity.Setmeal;
import com.lemon.reggie.pojo.vo.CategoryVO;
import com.lemon.reggie.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.reggie.service.DishService;
import com.lemon.reggie.service.SetmealService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author Lemon
 * @since 2022-05-23
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private DishService dishService;
    @Resource
    private SetmealService setmealService;

    /**
     * 分类分页
     * @param currentPage
     * @param pageSize
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.lemon.reggie.pojo.entity.Category>
     * @author Guannankun
     * @since 2022/8/8
     */
    @Override
    public IPage<Category> pageCategory(Long currentPage, Long pageSize) {
        IPage<Category> iPage = new Page<>(currentPage, pageSize);
        return this.page(iPage, new LambdaQueryWrapper<Category>().orderByAsc(Category::getSort));
    }

    /**
     * 添加分类
     * @param categoryDTO
     * @return void
     * @author Guannankun
     * @since 2022/8/8
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCategory(CategoryDTO categoryDTO) {
        /*Category one = this.getOne(new LambdaQueryWrapper<Category>().eq(Category::getName, categoryDTO.getName()));
        if (Objects.nonNull(one)) {
            throw new BusinessException(categoryDTO.getName() + "分类已存在！");
        }*/

        this.save(Category.builder()
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                .type(categoryDTO.getType())
                .build());
    }

    /**
     * 删除分类
     * @param id
     * @return void
     * @author Guannankun
     * @since 2022/8/8
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCategory(String id) {
        //当前分类有菜品或者套餐关联时，不能删除
        int count1 = dishService.count(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, Long.parseLong(id)));
        if (count1 > 0) {
            throw new BusinessException("当前分类有关联的菜品，不能删除！");
        }

        int count2 = setmealService.count(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, Long.parseLong(id)));
        if (count2 > 0) {
            throw new BusinessException("当前分类有关联的套餐，不能删除！");
        }

        this.removeById(Long.parseLong(id));
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return void
     * @author Guannankun
     * @since 2022/8/8
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editCategory(CategoryDTO categoryDTO) {
        if (Objects.isNull(categoryDTO.getId())) {
            throw new BusinessException("分类id不能为空！");
        }

        /*Category one = this.getOne(new LambdaQueryWrapper<Category>()
                .eq(Category::getName, categoryDTO.getName()).ne(Category::getId, categoryDTO.getId()));
        if (Objects.nonNull(one)) {
            throw new BusinessException(categoryDTO.getName() + "分类已存在！");
        }*/

        this.updateById(Category.builder()
                .id(categoryDTO.getId())
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                .build());
    }

    /**
     * 获取所有指定类型的分类
     * @param type
     * @return java.util.List<com.lemon.reggie.pojo.vo.CategoryVO>
     * @author Guannankun
     * @since 2022/8/9
     */
    @Override
    public List<CategoryVO> listCategory(Integer type) {
        List<Category> list = this.list(new LambdaQueryWrapper<Category>()
                .eq(type != null, Category::getType, type)
                .orderByAsc(Category::getSort));
        if (CollectionUtil.isNotEmpty(list)) {
            return list.stream().
                    map(category -> CategoryVO
                                    .builder()
                                    .id(category.getId())
                                    .name(category.getName())
                                    .type(category.getType())
                                    .build())
                    .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
